package com.ns002.yigou.quote;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.opslab.util.DateUtil;
import com.opslab.util.redis.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.ImageBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.Resource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Arrays;

/**
 * @author xu
 *
 * http://blog.csdn.net/javahighness/article/details/53055149 参考
 *
 * 主程序
 */
@SpringBootApplication
@ServletComponentScan                                              //扫描使用注解方式的Servlet

//这个扫描注解的路径设置,千万千万不能设置成com
//下面我出现问题的时候就是因为这里设置成com了,导致fastjson的fastJsonpResponseBodyAdvice这个方法也被开启了,
//所以要配置下面说的jsonp的问题 各位朋友千万注意...如果不需要jsonp,这里千万不能懒省事...
@ComponentScan(basePackages="com.ns002.yigou.quote")               //组件扫描
@EntityScan(basePackages="com.ns002.yigou.quote.entity")           //实体扫描
@EnableJpaRepositories(basePackages="com.ns002.yigou.quote.dao")   //dao扫描
@EnableTransactionManagement                                       //事务控制
@EnableScheduling                                                  //定时任务调度
public class Application {

    /**
     * @SpringBootApplication注解的类一定要放在自定义包下且属于自定义包的
     *
     * @SpringBootApplication is a convenience annotation that adds all of the following:
     * @SpringBootApplication是一个方便的注解，增加了所有的以下内容：
     *
     * @Configuration tags the class as a source of bean definitions for the application context.
     * @Configuration 标记一个类来作为bean定义的应用程序上下文的资源
     *
     * @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
     * @EnableAutoConfiguration告诉Spring Boot开始加载基于类路径下的配置信息、beans、各种属性配置。
     *
     *  Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds
     *  it automatically when it sees spring-webmvc on the classpath. This flags the application as a web
     *  application and activates key behaviors such as setting up a DispatcherServlet
     *  通常你会添加@EnableWebMvc为Spring MVC的应用程序，但是当Spring Boot在classpath下检索到spring-webmvc时，
     *  spring boot会自动添加。这标志该应用程序作为Web应用程序，并激活关键行为，如设立的DispatcherServlet
     *
     * @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
     * @ComponentScan 告诉Spring寻找其他组件，配置，以及业务层类,最前面才能加载到所有的类。
     */
    public static void main(String[] args) {

//      http://patorjk.com/software/taag/#p=display&f=Graffiti&t=yigou-quote
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);

    }


    /**
     * 保持spring上下文
     * @param context
     * @return
     */
    @Bean
    public SpringContextHolder springContextHolder(@Autowired ApplicationContext context){
        SpringContextHolder holder = new SpringContextHolder();
        holder.setApplicationContext(context);

        return holder;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(@Autowired JedisConnectionFactory jedisConnectionFactory){
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        return template;
    }

    @Bean
    public RedisTemplate cacheRedisTemplate(@Autowired JedisConnectionFactory jedisConnectionFactory){
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        StringRedisSerializer serializer = new StringRedisSerializer();
        template.setKeySerializer(serializer);
        template.setHashKeySerializer(serializer);

        return template;
    }


}
